package net.alan.modules.bean;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.List;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/23
 */
public class PageModel {
    private long count;//总数
    private List data;//数据
    private int page;//第几页
    private int limit;//条码
    private int code = 0;
    private String msg = "";
    private String search;//搜索内容

    public PageModel() {
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public void setData(List data) {
        this.count = data.size();
        this.data = data;
    }

    public List getData() {
        return data;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public long getCount() {
        return count;
    }

    public Pageable getPageable() {
        return new PageRequest(page - 1, limit,new Sort(Sort.Direction.DESC,"id"));
    }

    /**
     * 获取分页信息
     *
     * @param direction
     * @param field
     * @return
     */
    public Pageable getPageable(Sort.Direction direction, String field) {
        return new PageRequest(page - 1, limit, new Sort(direction, field));
    }

    public PageModel toPageModel(Page page) {
        this.setData(page.getContent());
        this.setCount(page.getTotalElements());
        return this;
    }
    public PageModel toPageModel(Page page,List list) {
        this.setData(list);
        this.setCount(page.getTotalElements());
        return this;
    }
}
